<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form autocomplete="off">
    姓名：<input type="text" id="username">
    <span id="uSpan"></span>
    <br>
    密码：<input type="password" id="password">
    <br>
    <input type="submit" value="注册">
</form>
</body>
<script src="/js/jquery-3.3.1.min.js"></script>
<script>
    $("#username").blur(function () {
       let username= $("#username").val();
       $.ajax(
           {
               url:"servlet",
               async:true, //没有双引号的true或者false
               data:"username="+username,
               type:"post",
               dataType:"text",
               success:function (data) {
                   $("#uSpan").html(data);
               },
               error:function () {
                   alert("操作失败。。。");
               }
           });
    });
 /* document.getElementById("username").onblur = function(){

       let xmlHttp= new XMLHttpRequest();

       let username= document.getElementById("username").value;

       // let username = $("#username").val();
        xmlHttp.open("GET","servlet?username="+username,true);

        xmlHttp.send();

        xmlHttp.onreadystatechange = function () {

            if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {

            document.getElementById("uSpan").innerHTML=xmlHttp.responseText;
            }
        }
    }*/
  /*  //1.为姓名绑定失去焦点事件
  document.getElementById("username").onblur = function() {
      //2.创建XMLHttpRequest核心对象
      let xmlHttp = new XMLHttpRequest();

      //3.打开链接
      let username = document.getElementById("username").value;
      xmlHttp.open("GET","servlet?username="+username,true);
      //xmlHttp.open("GET","userServlet?username="+username,false);

      //4.发送请求
      xmlHttp.send();

      //5.处理响应
      xmlHttp.onreadystatechange = function() {
          //判断请求和响应是否成功
          if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
              //将响应的数据显示到span标签
              document.getElementById("uSpan").innerHTML = xmlHttp.responseText;
          }
      }
  }
*/
</script>
</html>